package net.takela.dict.model;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import net.takela.common.web.model.Model;

import java.io.IOException;
import java.io.Serial;
import java.util.*;

/**
 *
 * @author baboy
 * @since 1.0.0 07/12/2016
 */
@Getter
@Setter
public class SysConf extends Model {

    /**
     *
     */
    @Serial
    private static final long serialVersionUID = -3373025212343942445L;
    /**
     *
     */
    public static String MODULE_SYS = "sys";
    /**
     *
     */
    public static String MODULE_CLIENT = "client";

    /**
     *
     */
    public static class ValueType{
        /**
         *
         */
        public static String STRING = "String";
        /**
         *
         */
        public static String INT = "Integer";
        /**
         *
         */
        public static String LONG = "Long";
        /**
         *
         */
        public static String FLOAT = "Float";
        /**
         *
         */
        public static String LIST = "List";
        /**
         *
         */
        public static String MAP = "HashMap";
    }

    /**
     *
     */
    private Integer id;
    /**
     *
     */
    private String module;
    /**
     *
     */
    private String title;
    /**
     *
     */
    private String note;
    /**
     *
     */
    private String key;
    /**
     *
     */
    private String value;
    /**
     *
     */
    private String valueType;
    /**
     *
     */
    private Integer createUid;
    /**
     *
     */
    private Date createTime;
    /**
     *
     */
    private Integer updateUid;
    /**
     *
     */
    private Date updateTime;


    /**
     *
     * @return 泛型
     */
    public Object getTypedValue(){
        if (value == null){
            return value;
        }
        Object val = value;
        if (ValueType.INT.equalsIgnoreCase(valueType)){
            val = Integer.parseInt(value);
        }
        if (ValueType.LONG.equalsIgnoreCase(valueType)){
            val = Long.parseLong(value);
        }
        if (ValueType.FLOAT.equalsIgnoreCase(valueType)){
            val = Float.parseFloat(value);
        }
        if (ValueType.MAP.equalsIgnoreCase(valueType) ||
                ValueType.LIST.equalsIgnoreCase(valueType)){
            ObjectMapper mapper = new ObjectMapper();
            try {
                if(value.startsWith("[")){
                    val = mapper.readValue(value, ArrayList.class);
                }else if(value.startsWith("{")){
                    val = mapper.readValue(value, HashMap.class);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return val;
    }

    /**
     *
     * @return map类型数据
     */
    public Map getMapValue() {
        Object val = getTypedValue();
        if (val != null && val instanceof Map){
            return (Map)val;
        }
        return null;
    }

    /**
     *
     * @return list类型数据
     */
    public List getListValue() {
        Object val = getTypedValue();
        if (val != null && val instanceof List){
            return (List)val;
        }
        return null;
    }

    /**
     *
     * @return long 类型数据
     */
    public Long getLongValue() {
        Object val = getTypedValue();
        if (val != null){
            try {
                Long v = Long.parseLong(val.toString());
                return v;
            }catch (Exception e){

            }
        }
        return null;
    }

    /**
     *
     * @return int类型数据
     */
    public Integer getIntegerValue() {
        Object val = getTypedValue();
        if (val != null){
            try {
                Integer v = Integer.parseInt(val.toString());
                return v;
            }catch (Exception e){

            }
        }
        return null;
    }

    /**
     *
     * @return float类型数据
     */
    public Float getFloatValue() {
        Object val = getTypedValue();
        if (val != null){
            try {
                Float v = Float.parseFloat(val.toString());
                return v;
            }catch (Exception e){

            }
        }
        return null;
    }
}
